//
//  BaseViewController.m
//  TeacherPro
//
//  Created by DCQ on 2017/4/24.
//  Copyright © 2017年 ZNXZ. All rights reserved.
//

#import "BaseViewController.h"
#import "UIImage+YYWebImage.h"
@interface BaseViewController ()<UINavigationControllerDelegate>

@end

@implementation BaseViewController
- (void)loadView
{
    [super loadView];
 
    self.view.backgroundColor = BACKGROUND_COLOR;
  
    self.edgesForExtendedLayout = [self getViewRect];
    
    self.extendedLayoutIncludesOpaqueBars = [self getLayoutIncludesOpaqueBars];
    self.automaticallyAdjustsScrollViewInsets = [self getAutomaticallyAdjustsScrollViewInsets];
}

- (BOOL)getAutomaticallyAdjustsScrollViewInsets{
    
    return YES;
}
- (void)configNavigationBar{
    
    UIColor * imageColor = nil;
    UIColor * titleColor = nil;
  
    if ([self getNavBarBgHidden]) {
        imageColor = [UIColor clearColor];
        titleColor = [UIColor clearColor];
    }else{
        imageColor = [UIColor whiteColor];
        titleColor =  [UIColor clearColor];
    }
  
    self.navigationController.navigationBar.translucent = [self getTranslucent];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage yy_imageWithColor:imageColor] forBarMetrics:UIBarMetricsDefault];
    
//    self.navigationController.navigationBar.barTintColor = imageColor;
}
//是否透明
- (BOOL)getTranslucent{
    return NO;
    
}
- (void)setupViewFrameHeight:(CGFloat )viewHeight{

    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, viewHeight);
    
}
- (void)confighViewFrame{
    CGFloat  viewHeight = 0;
    CGFloat tempBottomSpacing = 0;
    if (IS_IPHONE_X) {
        tempBottomSpacing = TABBAR_HEIGHT - 44.f;
    }
    if ([self getNavBarBgHidden]) {
   
        viewHeight = self.view.frame.size.height -tempBottomSpacing;
        
    }else{
       
        if ([self getViewRect] == UIRectEdgeNone) {
            viewHeight = self.view.frame.size.height - NAVIGATOR_HEIGHT-tempBottomSpacing;
        }else if ([self getViewRect] == UIRectEdgeAll){
            viewHeight = self.view.frame.size.height- tempBottomSpacing;
        } 
    }
    [self setupViewFrameHeight:viewHeight];
}
//  如果导航条不是隐藏的 UIRectEdgeNone  为下移64高度
// 如果导航条隐藏的 UIRectEdgeAll  需要要设置  extendedLayoutIncludesOpaqueBars 属性 为YES View显示在导航栏下面挡住一部分
- (UIRectEdge)getViewRect{

    return UIRectEdgeNone;
}

//    如果导航栏是不透明的, 默认UIViewController的View就会往下移, 在导航栏下显示, 如果不想往下移, YES 全屏View显示在导航栏挡住  NO 为下移64高度 View不会被导航栏挡住
- (BOOL )getLayoutIncludesOpaqueBars{
    
    return YES;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航条代理
//    self.navigationController.delegate = self;
    
    // Do any additional setup after loading the view.
    
    if ([self isShowBackItem]) {
        UIImage *barBackBtnImg = [[UIImage imageNamed:@"btn_fanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]   ;
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:barBackBtnImg style:UIBarButtonItemStylePlain target:self action:@selector(backViewController)];
        self.navigationItem.leftBarButtonItem = backItem;
    }
 

    [self confighViewFrame];
    [self navigationConfig];
}


/**
 导航栏的各种配置
 */
- (void)navigationConfig {
    
    
    //navigationBar下面的黑线隐藏掉
    UIImage * shadowImage = nil;
    if ([self getNavBarBgHidden]) {
        shadowImage = [UIImage  new];
    }else{
        shadowImage = [UIImage yy_imageWithColor:UIColorFromRGB(0xECEEEE)];
    }
    
    [self.navigationController.navigationBar setShadowImage:shadowImage];
  
    [self configNavigationBar];
   
}

- (BOOL )isShowBackItem{
  
    return YES;
}
- (BOOL)prefersStatusBarHidden

{
    return NO;
   
}
-  (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self navigationConfig];
  
}

- (BOOL)getNavBarBgHidden{

    return NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//当界面被刷新后，调用ViewController的“viewDidLayoutSubviews”
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
}
#pragma mark - 工具类方法    ////////////////////////////////////////////////////////////////////////////
- (void)backAction:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)pushViewController:(UIViewController *)viewController
{
     
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)presentViewController:(UIViewController *)viewController modalTransitionStyle:(UIModalTransitionStyle)style completion:(void (^)(void))completion
{
    if (viewController)
    {
        viewController.modalTransitionStyle = style;
        
        [self presentViewController:viewController animated:YES completion:completion];
    }
}


- (void)setNavigationItemTitle:(NSString *)titleStr
{
    [self setNavigationItemTitle:titleStr titleFont:[UIFont systemFontOfSize:18] titleColor:UIColorFromRGB(0x333333)];
}

- (void)setNavigationItemTitle:(NSString *)title titleFont:(UIFont *)font titleColor:(UIColor *)color
{
    /*
     CGSize size = [title sizeWithFont:font constrainedToSize:CGSizeMake(IPHONE_WIDTH, 44) lineBreakMode:NSLineBreakByWordWrapping];
     
     UILabel *navTitleLabel = InsertLabelWithShadowAndContentOffset(nil, CGRectMake(0, 0, size.width, size.height), NSTextAlignmentCenter, title, font, color, NO, IOS7 ? NO : YES, [UIColor darkGrayColor], CGSizeMake(0, -1), CGSizeMake(0, 0));
     self.navigationItem.titleView = navTitleLabel;
     */
    
    self.navigationItem.title = title;
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font};
}

- (void)backViewController
{
    
    NSArray *viewControllers = [self.navigationController viewControllers];
    
    // 根据viewControllers的个数来判断此控制器是被present的还是被push的
    if (1 <= viewControllers.count && 0 < [viewControllers indexOfObject:self])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}
- (void)showAlertView:(NSString *)msg withConfirmAction:(void (^ __nullable)(UIAlertAction *action))confirmHandler withCancelAction:(void (^ __nullable)(UIAlertAction *action))cancelHandler{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                   message:msg
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:confirmHandler];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:cancelHandler];
    
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}
- (void)showAlertView:(NSString *)msg withConfirmAction:(void (^ __nullable)(UIAlertAction *action))confirmHandler{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                   message:msg
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:confirmHandler];
 
    
    [alert addAction:defaultAction];
 
    [self presentViewController:alert animated:YES completion:nil];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
